package com.qidian.by.sso.controller;

import com.qidian.by.sso.util.JWTUtil;
import com.qidian.by.util.pojo.EsResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Author: 小可怎么都睡不够
 * Date: 2024-05-16 09:55
 * company: 起点编程
 **/
@RestController
@RequestMapping("/code")
public class CodeController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private JWTUtil jwtUtil;
    @GetMapping("/getToken")
    public EsResult getCodeToken(){
        // 生成token
        String token = jwtUtil.getToken(new HashMap<>(), 60 * 60);
        // 主要目标是生成一个唯一的标志，使用UUID也可以
        return EsResult.ok(token);
    }
    @RequestMapping("/getCode")
    public void createCode(String codeToken, HttpServletResponse response) throws IOException {
        // 绘制图片
        // 准备一个图片缓冲区
        int width = 100;
        int height = 40;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 从图片上获取一只画笔
        Graphics g = image.getGraphics();
        // 填充图片
        g.setColor(Color.WHITE);
        g.fillRect(0,0,width,height);// 填充  实心的
        // 绘制边框
        g.setColor(Color.BLACK);
        g.drawRect(0,0,width,height);// 绘制  空心的
        // 生成验证码
        String str = "qwertyuiopasdfghjklzxcvbnm123456789QWERTYUIOPLKJHGFDSAMNBVCXZ852963741";
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < 4; i++){
            char c = str.charAt(r.nextInt(str.length()));
            sb.append(c);
            // 绘制验证码
            g.setColor(new Color(50+r.nextInt(75),50+r.nextInt(75),50+r.nextInt(75)));
            // 设置字体
            g.setFont(new Font("宋体",Font.BOLD,30));
            // 绘制文本
            g.drawString(c + "",(i * (width / 4) + r.nextInt(10)),(height - 10) + r.nextInt(10));
        }
        // 绘制干扰线
        for (int i = 0; i < 10; i++){
            g.setColor(new Color(50+r.nextInt(105),50+r.nextInt(105),50+r.nextInt(105)));
            g.drawLine(r.nextInt(width),r.nextInt(height),r.nextInt(width),r.nextInt(height));
        }
        // 将验证码放入redis中,key就是传递的codeToken
        stringRedisTemplate.opsForValue().set(codeToken,sb.toString());
        // 设置key的时间为5分钟
        stringRedisTemplate.expire(codeToken,5, TimeUnit.MINUTES);
        // 输出图片到客户端
        ServletOutputStream out = response.getOutputStream();// 这个流就是指向客户端的流
        // 使用工具类输出
        ImageIO.write(image,"JPEG",out);
    }
}
